package com.heima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.boot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/kafka")
public class HelloController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;


    @GetMapping("/send")
    public String send() {
        User user = new User();
        user.setName("丁光军");
        user.setAge(30);

        //发送消息
        kafkaTemplate.send("itcast-topic", JSON.toJSONString(user));
        return "消息发送成功";
    }
}
